package jsj.myjui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

//键盘输入功能需要点击输入栏后进行输入，回车会识别为‘=’

public class cal implements ActionListener,KeyListener{

    JFrame frame = new JFrame("计算器");
    JPanel panel = new JPanel(new GridLayout(4,4));
    JTextField textfield = new JTextField(20);
    Boolean start = true;
    String lastcommand = "=";
    double result = 0;
    String[] name ={"7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/"};
    JButton[] buttons = new JButton[name.length];

    public cal() {
        frame.add(textfield, BorderLayout.NORTH);

        for(int i = 0; i < buttons.length; i++) {
            buttons[i] = new JButton(name[i]);
            buttons[i].addActionListener(this);
            panel.add(buttons[i]);
        }
        textfield.setEditable(false);
        textfield.addKeyListener(this);
        frame.add(panel, BorderLayout.CENTER);
        frame.setBounds(100,100,400,300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public void calculate(double x){
        if(lastcommand.equals("+")){
            result += x;
        }
         if(lastcommand.equals("-")){
            result -= x;
        }
         if(lastcommand.equals("*")){
            result *= x;
        }
         if(lastcommand.equals("/")){
            result /= x;
        }
         if(lastcommand.equals("=")){
            result = x;
        }
         textfield.setText(String.valueOf(result));
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String input = e.getActionCommand();
        if(input.equals("+")||input.equals("-")||input.equals("*")||input.equals("/")||input.equals("=")){
            if(start){
                if(input.equals("-")){
                    textfield.setText(input);
                    start = false;
                }else {
                    lastcommand = input;
                }
            }
            else {
                calculate(Double.parseDouble(textfield.getText()));
                lastcommand = input;
                start = true;
            }
        }
        else{
            if(start){
                textfield.setText("");
                start = false;
            }
            textfield.setText(textfield.getText()+input);
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {
        char input = e.getKeyChar();
        if((input<=47&&input>=42&&input!=44)||input==10){
            if(start){
                if(input == 45){
                    textfield.setText(String.valueOf(input));
                    start = false;
                }
                if(input == 10){
                    lastcommand = "=";
                } else {
                    lastcommand = String.valueOf(input);
                }
            }
            else {
                calculate(Double.parseDouble(textfield.getText()));
                lastcommand = String.valueOf(input);
                start = true;
            }
        }
        else if(input>=48&&input<=57){
            if(start){
                textfield.setText("");
                start = false;
            }
            textfield.setText(textfield.getText()+input);
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
    public static void main(String[] args) {
        new cal();
    }
}
